package com.wb3.organizer.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.wb3.organizer.model.Account;
import com.wb3.organizer.model.Task;
import com.wb3.organizer.service.TaskService;

public class TaskAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger log = Logger.getLogger(Task.class);

	private Map<String, Object> session;

	private Task task;

	private List<Task> tasks;

	private TaskService service;

	private String operate;

	private String msg;

	public void setSession(Map<String, Object> inSession) {
		this.session = inSession;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public TaskService getService() {
		return service;
	}

	public void setService(TaskService service) {
		this.service = service;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String create() {
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("TaskAction.create()...\n");

		Account account = (Account) session.get("account");
		task.setAccount(account);
		task.setCreatedDT(new Date().getTime());
		service.saveTask(task);
		
		this.setOperate("taskCreateOK");
		this.setMsg("任务创建成功!!!");
		log.debug("TaskAction.create() Done\n");
		return SUCCESS;
	}
	
	public String update(){
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("TaskAction.update()...\n");
		
		Account account = (Account) session.get("account");
		task.setAccount(account);
		task.setCreatedDT(new Date().getTime());
		service.updateTask(task);
		
		this.setOperate("taskUpdateOK");
		this.setMsg("任务编辑成功!!!");
		log.debug("TaskAction.update() Done\n");
		return SUCCESS;
	}
	
	public String delete(){
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("TaskAction.delete()...\n");
		
		service.delete(task);
		
		this.setOperate("taskDeleteOK");
		this.setMsg("任务删除成功!!!");
		log.debug("TaskAction.delete() Done\n");
		return SUCCESS;
	}

	public String list() {
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("TaskAction.list()...\n");

		Account account = (Account) session.get("account");
		tasks = service.getTaskByTime(account, false);

		log.debug("TaskAction.list() Done\n");
		return SUCCESS;
	}

	public String forward() {
		if (operate == null) {
			return SUCCESS;
		} else {
			if (operate.equals("create")) {
				return "create";
			} else if (operate.equals("update")) {
				task = service.getNote(task.getTask_id());
				return "update";
			} else if (operate.equals("delete")) {
				return "delete";
			} else {
				return ERROR;
			}
		}
	}

}
